<?php

/**
 *    购物车控制器，负责会员购物车的管理工作，她与下一步售货员的接口是：购物车告诉售货员，我要买的商品是我购物车内的商品
 *
 *    @author    Garbin
 */
class CartApp extends MallbaseApp {

    /**
     *    列出购物车中的商品
     *
     *    @author    Garbin
     *    @return    void
     */
    function index() {
        $store_id = isset($_GET['store_id']) ? intval($_GET['store_id']) : 0;
        $carts = $this->_get_carts($store_id);
        $this->_curlocal(
                LANG::get('cart')
        );
        $this->_config_seo('title', Lang::get('confirm_goods') . ' - ' . Conf::get('site_title'));

        if (empty($carts)) {
            $this->_cart_empty();
            return;
        }

        /*    感兴趣的商品 */
        $goods_mod = &m('goods');
        $gst_mod = &m('goodsstatistics');
        $interest = $goods_mod->find(array(
            'conditions' => '',
            'join' => 'has_goodsstatistics',
            'order' => 'views desc,collects desc, sales desc',
            'fields' => 'g.goods_id,goods_name,price,sales,default_image',
            'limit' => 6
        ));
        $this->assign('interest', $interest);
        /* end */

        $this->assign('carts', $carts);
        $this->display('cart.index.html');
    }

    /**
     *    放入商品(根据不同的请求方式给出不同的返回结果)
     *
     *    @author    Garbin
     *    @return    void
     */
    function add() {
        $promotions_id = isset($_GET['gid'])? intval($_GET['gid']) : 0;
        $spec_id = isset($_GET['spec_id']) ? intval($_GET['spec_id']) : 0;
        $quantity = isset($_GET['quantity']) ? intval($_GET['quantity']) : 0;
        if (!$spec_id || !$quantity) {
            return;
        }

        /* 是否有商品 */
        $spec_model = & m('goodsspec');
        $spec_info = $spec_model->get(array(
            'fields' => 'g.spec_qty,g.store_id, g.if_open, g.goods_id, g.goods_name, g.spec_name_1, g.spec_name_2, g.spec_name_3, g.spec_name_4, g.default_image, gs.spec_1, gs.spec_2, gs.spec_3, gs.spec_4, gs.stock, gs.price',
            'conditions' => $spec_id,
            'join' => 'belongs_to_goods',
        ));

        if (!$spec_info) {
            $this->json_error('no_such_goods');
            /* 商品不存在 */
            exit;
        }

        /* 如果是自己店铺的商品，则不能购买 */
        if ($this->visitor->get('manage_store')) {
            if ($spec_info['store_id'] == $this->visitor->get('manage_store')) {
                $this->json_error('can_not_buy_yourself');

                exit;
            }
        }

        $cart_mod = & m('cart');
		$item_info = $cart_mod->get("spec_id={$spec_id} AND session_id='" . SESS_ID . "'");

        if ($quantity > $spec_info['stock']) {
            $this->json_error('no_enough_goods');
            exit;
        }




        $spec_1 = $spec_info['spec_name_1'] ? $spec_info['spec_name_1'] . ':' . '<img src="'.str_replace('goodsspec/','goodsspec/spec_',$spec_info['spec_1']).'">' : $spec_info['spec_1'];
        $spec_2 = $spec_info['spec_name_2'] ? $spec_info['spec_name_2'] . ':' . $spec_info['spec_2'] : $spec_info['spec_2'];
		$spec_3 = $spec_info['spec_name_3'] ? $spec_info['spec_name_3'] . ':' . $spec_info['spec_3'] : $spec_info['spec_3'];
		$spec_4 = $spec_info['spec_name_4'] ? $spec_info['spec_name_4'] . ':' . $spec_info['spec_4'] : $spec_info['spec_4'];

        ($spec_info['spec_qty']>0) && $specification = $spec_1 . ' ' . $spec_2 . ' ' . $spec_3 . ' ' . $spec_4;

        //当用多个促销条件存在时候， 此方法用来处理具体使用哪一个价格
        $spec_info = $cart_mod->get_spec_price($spec_info,$this->visitor->get('user_id'),$promotions_id);
        
        //如果是团购商品则判断是否限购
        if($spec_info['goods_type']==GOODS_TYPE_GROUP){
                if($spec_info['max_per_user']){
                    //检查数量是否超过限定团购数量
                    $ordergoods_mod=& m('ordergoods');
                    $ordergoods_quantity=intval($ordergoods_mod->getOne("SELECT SUM(quantity) FROM ".$ordergoods_mod->table." ordergoods LEFT JOIN ".DB_PREFIX."order order_alias ON order_alias.order_id=ordergoods.order_id WHERE order_alias.status<>".ORDER_CANCELED." AND order_alias.buyer_id=".$this->visitor->get('user_id')." AND promotions_id=".$promotions_id." AND goods_type=".GOODS_TYPE_GROUP." AND spec_id=".$spec_id));
                    //购物车中相同商品不同规格的商品
                    $same_goods=intval($cart_mod->getOne('SELECT SUM(quantity) FROM '.$cart_mod->table.' WHERE goods_id='.$spec_info['goods_id'].' AND session_id="'.SESS_ID.'"')); 
                    if(($ordergoods_quantity+$quantity+$same_goods)>$spec_info['max_per_user']){
                        $this->json_error('特卖特价商品，每人限购'.$spec_info['max_per_user'].'件');
                        exit;
                    }
                }
                
        }
if($_GET['goods']!='single'){
        /* 是否添加过 */
        
        if (!empty($item_info)) {
        $cart_mod->edit($item_info['rec_id'], array(
			'price' => $spec_info['price'],
            'quantity' => $quantity,
        ));
        $cart_status = $this->_get_cart_status();



        $this->json_result(array(
            'cart' => $cart_status['status'], //返回购物车状态
                ), 'addto_cart_successed');

            return;
        }
    }
        /* 将商品加入购物车 */
        $cart_item = array(
            'user_id' => $this->visitor->get('user_id'),
            'session_id' => SESS_ID,
            'store_id' => $spec_info['store_id'],
            'spec_id' => $spec_id,
            'goods_id' => $spec_info['goods_id'],
            'goods_name' => addslashes($spec_info['goods_name']),
            'specification' => addslashes(trim($specification)),
            'price' => $spec_info['price'],
            'quantity' => $quantity,
            'goods_image' => addslashes($spec_info['default_image']),
            'promotions_id' => $spec_info['promotions_id'],
            'goods_old_price' => $spec_info['goods_old_price'],
            'goods_type' => $spec_info['goods_type'],
        );
if($_GET['goods']!='single'){
        /* 添加并返回购物车统计即可 */
        $cart_model = & m('cart');
        $cart_model->add($cart_item);

        $cart_status = $this->_get_cart_status();

        /* 更新被添加进购物车的次数 */
        $model_goodsstatistics = & m('goodsstatistics');
        $model_goodsstatistics->edit($spec_info['goods_id'], 'carts=carts+1');

        $this->json_result(array(
            'cart' => $cart_status['status'], //返回购物车状态
                ), 'addto_cart_successed');
}else{
    return array(array_merge($cart_item,array('stock'=>$spec_info['stock'])));
}
    }
function json_error($msg){
	if($_GET['goods']!='single'){
		parent::json_error($msg);
	}else{
		$this->show_warning($msg);
	}
}
    /**
     *    丢弃商品
     *
     *    @author    Garbin
     *    @return    void
     */
    function drop() {
        /* 传入rec_id，删除并返回购物车统计即可 */
        $rec_id = isset($_GET['rec_id']) ? intval($_GET['rec_id']) : 0;
        if (!$rec_id) {
            return;
        }

        /* 从购物车中删除 */
        $cart_mod = & m('cart');
        $droped_rows = $cart_mod->drop('rec_id=' . $rec_id . ' AND session_id=\'' . SESS_ID . '\'', 'store_id');
        if (!$droped_rows) {
            return;
        }

        /* 返回结果 */
        $dropped_data = $cart_mod->getDroppedData();
        $store_id = $dropped_data[$rec_id]['store_id'];
        $cart_status = $this->_get_cart_status();
        $this->json_result(array(
            'cart' => $cart_status['status'], //返回总的购物车状态
            'amount' => $cart_status['carts'][$store_id]['amount']   //返回指定店铺的购物车状态
                ), 'drop_item_successed');
    }

    /**
     *    更新购物车中商品的数量，以商品为单位，AJAX更新
     *
     *    @author    Garbin
     *    @param    none
     *    @return    void
     */
    function update() {
        $spec_id = isset($_GET['spec_id']) ? intval($_GET['spec_id']) : 0;
        $quantity = isset($_GET['quantity']) ? intval($_GET['quantity']) : 0;
        if (!$spec_id || !$quantity) {
            /* 不合法的请求 */
            return;
        }

        /* 判断库存是否足够 */
        $spec_model = & m('goodsspec');
        $spec_info = $spec_model->get(array(
            'fields' => 'g.spec_qty,g.store_id, g.if_open, g.goods_id, g.goods_name, g.spec_name_1, g.spec_name_2, g.spec_name_3, g.spec_name_4, g.default_image, gs.spec_1, gs.spec_2, gs.spec_3, gs.spec_4, gs.stock, gs.price',
            'conditions' => $spec_id,
            'join' => 'belongs_to_goods',
        ));
        if (empty($spec_info)) {
            /* 没有该规格 */
            $this->json_error('no_such_spec');
            return;
        }

        if ($quantity > $spec_info['stock']) {
            /* 数量有限 */
            $this->json_error('no_enough_goods');
            return;
        }

        /* 修改数量 */
        $where = "spec_id={$spec_id} AND session_id='" . SESS_ID . "'";
        $cart_mod = & m('cart');


        /* 获取购物车中的信息，用于获取价格并计算小计 */
		$cart_spec_info_ = $cart_mod->get($where);
        $cart_spec_info =$cart_mod->get_spec_price($spec_info,$this->visitor->get('user_id'),(($cart_spec_info_['promotions_id'] && ($cart_spec_info_['goods_type']==GOODS_TYPE_GROUP))?$cart_spec_info_['promotions_id']:0),$quantity);
 
        if (empty($cart_spec_info)) {
            /* 并没有添加该商品到购物车 */
            return;
        }

        $store_id = $cart_spec_info['store_id'];
        //如果是团购商品则判断是否限购
        if($cart_spec_info['goods_type']==GOODS_TYPE_GROUP){
            $ju_mod=& m('ju');
            $max_per_user=$ju_mod->getOne('SELECT max_per_user FROM '.$ju_mod->table." WHERE group_id=".$cart_spec_info['promotions_id']);
                if($max_per_user){
                    //检查数量是否超过限定团购数量
                    $ordergoods_mod=& m('ordergoods');
                    $ordergoods_quantity=intval($ordergoods_mod->getOne("SELECT SUM(quantity) FROM ".$ordergoods_mod->table." ordergoods LEFT JOIN ".DB_PREFIX."order order_alias ON order_alias.order_id=ordergoods.order_id WHERE order_alias.status<>".ORDER_CANCELED." AND order_alias.buyer_id=".$this->visitor->get('user_id')." AND promotions_id=".$cart_spec_info['promotions_id']." AND goods_type=".GOODS_TYPE_GROUP." AND spec_id=".$spec_id));
                    //购物车中相同商品不同规格的商品
                    $same_goods=intval($cart_mod->getOne('SELECT SUM(quantity) FROM '.$cart_mod->table.' WHERE goods_id='.$cart_spec_info['goods_id'].' AND rec_id<>'.$cart_spec_info_['rec_id'].' AND session_id="'.SESS_ID.'"'));
                    if(($ordergoods_quantity+$quantity+$same_goods)>$max_per_user){
                        $this->json_error('特卖特价商品，每人限购'.$max_per_user.'件');
                        return;
                    }
                }
        }
        /* 修改数量 */
        $cart_mod->edit($where, array(
            'quantity' => $quantity,
        ));

        /* 小计 */
        $subtotal = $quantity * $cart_spec_info['price'];

        /* 返回JSON结果 */
        $cart_status = $this->_get_cart_status();
        $this->json_result(array(
            'cart' => $cart_status['status'], //返回总的购物车状态
            'subtotal' => $subtotal, //小计
            'amount' => $cart_status['carts'][$store_id]['amount']  //店铺购物车总计
                ), 'update_item_successed');
    }

    /**
     *    获取购物车状态
     *
     *    @author    Garbin
     *    @return    array
     */
    function _get_cart_status() {
        /* 默认的返回格式 */
        $data = array(
            'status' => array(
                'quantity' => 0, //总数量
                'amount' => 0, //总金额
                'kinds' => 0, //总种类
            ),
            'carts' => array(), //购物车列表，包含每个购物车的状态
        );

        /* 获取所有购物车 */
        $carts = $this->_get_carts();
        if (empty($carts)) {
            return $data;
        }
        $data['carts'] = $carts;
        foreach ($carts as $store_id => $cart) {
            $data['status']['quantity'] += $cart['quantity'];
            $data['status']['amount'] += $cart['amount'];
            $data['status']['kinds'] += $cart['kinds'];
        }

        return $data;
    }

    /**
     *    购物车为空
     *
     *    @author    Garbin
     *    @return    void
     */
    function _cart_empty() {
        $goods_mod = &m('goods');
        $gst_mod = &m('goodsstatistics');
        $interest = $goods_mod->find(array(
            'conditions' => '',
            'join' => 'has_goodsstatistics',
            'order' => 'views desc,collects desc, sales desc',
            'fields' => 'g.goods_id,goods_name,price,sales,default_image',
            'limit' => 6
        ));
        $this->assign('interest', $interest);
        $this->display('cart.empty.html');
    }

    /**
     *    以购物车为单位获取购物车列表及商品项
     *
     *    @author    Garbin
     *    @return    void
     */
    function _get_carts($store_id = 0) {
        $carts = array();

        /* 获取所有购物车中的内容 */
        $where_store_id = $store_id ? ' AND cart.store_id=' . $store_id : '';

        /* 只有是自己购物车的项目才能购买 */
        $where_user_id = $this->visitor->get('user_id') ? " AND cart.user_id=" . $this->visitor->get('user_id') : '';
        $cart_model = & m('cart');
        $cart_items = $cart_model->find(array(
            'conditions' => 'session_id = \'' . SESS_ID . "'" . $where_store_id . $where_user_id,
            'fields' => 'this.*,store.store_name',
            'join' => 'belongs_to_store',
        ));
        if (empty($cart_items)) {
            return $carts;
        }
        $kinds = array();
		$cart_mod=& m('cart');
        $spec_model = & m('goodsspec');
        foreach ($cart_items as $item) {
			$specification='';
/* 更新购物车商品信息start */
                    $spec_info = $spec_model->get(array(
                        'fields' => 'g.spec_qty,g.store_id, g.if_open, g.goods_id, g.goods_name, g.spec_name_1, g.spec_name_2, g.spec_name_3, g.spec_name_4, g.default_image, gs.spec_1, gs.spec_2, gs.spec_3, gs.spec_4, gs.stock, gs.price',
                        'conditions' => 'spec_id='.$item['spec_id'].' AND if_show=1 AND closed=0',
                        'join' => 'belongs_to_goods',
                    ));
                    
                    if($spec_info){
                        $item2=array();
                        $spec_info = $cart_mod->get_spec_price($spec_info,$this->visitor->get('user_id'),(($item['promotions_id'] && ($item['goods_type']==GOODS_TYPE_GROUP))?$item['promotions_id']:0),$item['quantity']);
        $spec_1 = $spec_info['spec_name_1'] ? $spec_info['spec_name_1'] . ':' . '<img src="'.str_replace('goodsspec/','goodsspec/spec_',$spec_info['spec_1']).'">' : $spec_info['spec_1'];
        $spec_2 = $spec_info['spec_name_2'] ? $spec_info['spec_name_2'] . ':' . $spec_info['spec_2'] : $spec_info['spec_2'];
		$spec_3 = $spec_info['spec_name_3'] ? $spec_info['spec_name_3'] . ':' . $spec_info['spec_3'] : $spec_info['spec_3'];
		$spec_4 = $spec_info['spec_name_4'] ? $spec_info['spec_name_4'] . ':' . $spec_info['spec_4'] : $spec_info['spec_4'];

        ($spec_info['spec_qty']>0) && $specification = $spec_1 . ' ' . $spec_2 . ' ' . $spec_3 . ' ' . $spec_4;
                        $item['price']=$item2['price']=$spec_info['price'];
                        $item['promotions_id']=$item2['promotions_id'] = $spec_info['promotions_id'];
                        $item['goods_old_price']=$item2['goods_old_price'] = $spec_info['goods_old_price'];
                        $item['goods_type']=$item2['goods_type'] = $spec_info['goods_type'];
                        $item['specification']=$item2['specification'] = (trim($specification));
                        $item['goods_name']=$item2['goods_name'] = addslashes($spec_info['goods_name']);
                        //$item['quantity']=$item2['quantity']=$spec_info['quantity'];
                        $cart_mod->edit($item['rec_id'],$item2);
                    }else{
						$cart_mod->drop($item['rec_id']);
                        continue;
                    }
/* 更新购物车商品信息end */ 
            /* 小计 */
            $item['subtotal'] = $item['price'] * $item['quantity'];
            $kinds[$item['store_id']][$item['goods_id']] = 1;

            /* 以店铺ID为索引 */
            empty($item['goods_image']) && $item['goods_image'] = Conf::get('default_goods_image');
            $carts[$item['store_id']]['store_name'] = $item['store_name'];
            $carts[$item['store_id']]['amount'] += $item['subtotal'];   //各店铺的总金额
            $carts[$item['store_id']]['quantity'] += $item['quantity'];   //各店铺的总数量
            $carts[$item['store_id']]['goods'][] = $item;
        }

        foreach ($carts as $_store_id => $cart) {
            $carts[$_store_id]['kinds'] = count(array_keys($kinds[$_store_id]));  //各店铺的商品种类数
        }

        return $carts;
    }

    function meals_cart() {
        $spec_id = isset($_GET['spec_id']) ? intval($_GET['spec_id']) : 0;
        $quantity = isset($_GET['quantity']) ? intval($_GET['quantity']) : 0;
        $store_id = isset($_GET['store_id']) ? intval($_GET['store_id']) : 0;
        if (!$spec_id || !$store_id) {
            /* 不合法的请求 */
            return;
        }
        /* 判断库存是否足够 */
        $spec_mod = & m('goodsspec');
        $spec_info = $spec_mod->get($spec_id);
        if (empty($spec_info)) {
            /* 没有该规格 */
            $this->json_error('no_such_spec');
            return;
        }
        if ($quantity > $spec_info['stock']) {
            /* 数量有限 */
            $this->json_error('no_enough_goods');
            return;
        }
        if ($quantity > 0) {
            /* 修改数量 */
            $where = "store_id = {$store_id} AND spec_id={$spec_id} AND session_id='" . SESS_ID . "'";
            $cart_mod = & m('cart');

            /* 获取购物车中的信息，用于获取价格并计算小计 */
            $cart_spec_info = $cart_mod->get($where);

            if (empty($cart_spec_info)) {
                /* 没有加入购物车则 加入购物车 */
                $this->meals_add($spec_id, $quantity, $store_id);
            } else {
                /* 修改数量 */
                $cart_mod->edit($where, array(
                    'quantity' => $quantity,
                ));
            }
        } else {
            //小于0 则删除
            $this->meals_drop($spec_id, $quantity, $store_id);
        }

        /* 返回JSON结果 */
        $carts_list = $this->_get_carts($store_id);
        $carts = $carts_list[$store_id];


        $cartShowDiv = '';
        if ($carts['goods']) {
            $cartShowDiv = '
                            <div class="order-panel">
                                <div class="waves"></div>
                                <div class="order">
                                    <div class="order-tit clearfix"><span class="what">商品</span><span class="order-num">数量</span><span class="order-price">价格</span></div>
                                    <div class="order-list">
                                        <div id="content_1">
                                            <div class="scrollbar">
                                                <ul>
                ';
            foreach ($carts['goods'] as $key => $goods) {
                $cartShowDiv .='
                    <li>
                    <span class="order-name"><strong>' . $goods['goods_name'] . '</strong></span>
                    <span class="count-panel">
                    <span class="place-holder"><a href="javascript:void(0);" onclick="reduceToCart(' . $goods['spec_id'] . ')" class="i i-plus"></a></span>
                    <input type="text" class="result" onblur="" id="cart_amount_input_' . $goods['spec_id'] . '" value="' . $goods['quantity'] . '">
                    <span class="place-holder"><a href="javascript:void(0);" onclick="addToCart(' . $goods['spec_id'] . ')" class="i i-add"></a></span>
                    </span>
                    <span class="price-num">￥' . $goods['subtotal'] . '</span>
                    </li>
                ';
            }
            $cartShowDiv .='                                                
                </ul></div></div>
                <div class="sum clearfix"><span>&nbsp;数量:<strong>' . $carts['quantity'] . '</strong></span><span>&nbsp;合计:<strong>￥' . $carts['amount'] . '</strong></span></div>
                </div></div><div class="waves2"></div></div>';
        } else {
            $cartShowDiv = '<div class="i i-empty">购物车为空</div>';
        }

        $this->json_result(array(
            'carts' => $carts,
            'cartShowDiv' => $cartShowDiv
                ), 'update_item_successed');
    }

    function meals_add($spec_id, $quantity, $store_id) {
        if (!$spec_id || !$quantity) {
            exit;
        }

        /* 是否有商品 */
        $spec_model = & m('goodsspec');
        $spec_info = $spec_model->get(array(
            'fields' => 'g.spec_qty,g.store_id, g.if_open, g.goods_id, g.goods_name, g.spec_name_1, g.spec_name_2, g.spec_name_3, g.spec_name_4, g.default_image, gs.spec_1, gs.spec_2, gs.spec_3, gs.spec_4, gs.stock, gs.price',
            'conditions' => $spec_id,
            'join' => 'belongs_to_goods',
        ));

        if (!$spec_info) {
            $this->json_error('no_such_goods');
            /* 商品不存在 */
            exit;
        }

        /* 如果是自己店铺的商品，则不能购买 */
        if ($this->visitor->get('manage_store')) {
            if ($spec_info['store_id'] == $this->visitor->get('manage_store')) {
                $this->json_error('can_not_buy_yourself');
                exit;
            }
        }

        /* 是否添加过 */
        $cart_mod = & m('cart');
        $item_info = $cart_mod->get("store_id = {$store_id} AND spec_id={$spec_id} AND session_id='" . SESS_ID . "'");
        if (!empty($item_info)) {
            $this->json_error('goods_already_in_cart');
            exit;
        }

        if ($quantity > $spec_info['stock']) {
            $this->json_error('no_enough_goods');
            return;
        }

        $spec_1 = $spec_info['spec_name_1'] ? $spec_info['spec_name_1'] . ':' . '<img src="'.str_replace('goodsspec/','goodsspec/spec_',$spec_info['spec_1']).'">' : $spec_info['spec_1'];
        $spec_2 = $spec_info['spec_name_2'] ? $spec_info['spec_name_2'] . ':' . $spec_info['spec_2'] : $spec_info['spec_2'];
		$spec_3 = $spec_info['spec_name_3'] ? $spec_info['spec_name_3'] . ':' . $spec_info['spec_3'] : $spec_info['spec_3'];
		$spec_4 = $spec_info['spec_name_4'] ? $spec_info['spec_name_4'] . ':' . $spec_info['spec_4'] : $spec_info['spec_4'];

        ($spec_info['spec_qty']>0) && $specification = $spec_1 . ' ' . $spec_2 . ' ' . $spec_3 . ' ' . $spec_4;

        //当用多个促销条件存在时候， 此方法用来处理具体使用哪一个价格
        $spec_info = $cart_mod->get_spec_price($spec_info,$this->visitor->get('user_id'));

        /* 将商品加入购物车 */
        $cart_item = array(
            'user_id' => $this->visitor->get('user_id'),
            'session_id' => SESS_ID,
            'store_id' => $spec_info['store_id'],
            'spec_id' => $spec_id,
            'goods_id' => $spec_info['goods_id'],
            'goods_name' => addslashes($spec_info['goods_name']),
            'specification' => addslashes(trim($specification)),
            'price' => $spec_info['price'],
            'quantity' => $quantity,
            'goods_image' => addslashes($spec_info['default_image']),
        );

        /* 添加并返回购物车统计即可 */
        $cart_model = & m('cart');
        $cart_model->add($cart_item);

        /* 更新被添加进购物车的次数 */
        $model_goodsstatistics = & m('goodsstatistics');
        $model_goodsstatistics->edit($spec_info['goods_id'], 'carts=carts+1');
    }

    function meals_drop($spec_id, $quantity, $store_id) {
        /* spec_id，删除并返回购物车统计即可 */
        $spec_id = isset($_GET['spec_id']) ? intval($_GET['spec_id']) : 0;
        if (!$spec_id) {
            return;
        }

        /* 从购物车中删除 */
        $cart_mod = & m('cart');
        $droped_rows = $cart_mod->drop('spec_id=' . $spec_id . ' AND session_id=\'' . SESS_ID . '\'', 'store_id');
        if (!$droped_rows) {
            return;
        }
    }

    function meals_clear() {
        $cart_mod = & m('cart');
        $store_id = isset($_GET['store_id']) ? intval($_GET['store_id']) : 0;
        $droped_rows = $cart_mod->drop('store_id=' . $store_id . ' AND session_id=\'' . SESS_ID . '\'', 'store_id');
        if (!$droped_rows) {
            return;
        } else {
            $cartShowDiv = '<div class="i i-empty">购物车为空</div>';
        }
        $this->json_result(array(
            'carts' => $carts,
            'cartShowDiv' => $cartShowDiv
                ), 'update_item_successed');
    }

    function max_goods_cart() {
        $spec_ids = isset($_GET['goods_mix_ids']) ? $_GET['goods_mix_ids'] : 0;
        if (empty($spec_ids)) {
            return;
        }
        $spec_ids = explode(",", $spec_ids);
        $spec_model = & m('goodsspec');

        //总共价格
        $total_price = 0;
        $total_count = -1;

        $cart_mod = &m('cart');
        foreach ($spec_ids as $key => $spec_id) {

            $quantity = 1;
            if (!$spec_id) {
                continue;
            }

            /* 是否有商品 */
            $spec_model = & m('goodsspec');
            $spec_info = $spec_model->get(array(
                'fields' => 'g.spec_qty,g.store_id, g.if_open, g.goods_id, g.goods_name, g.spec_name_1, g.spec_name_2, g.spec_name_3, g.spec_name_4, g.default_image, gs.spec_1, gs.spec_2, gs.spec_3, gs.spec_4, gs.stock, gs.price',
                'conditions' => $spec_id,
                'join' => 'belongs_to_goods',
            ));

            if (!$spec_info) {
                continue;
            }

            //当用多个促销条件存在时候， 此方法用来处理具体使用哪一个价格
            $spec_info = $cart_mod->get_spec_price($spec_info,$this->visitor->get('user_id'));
            $total_price += $spec_info['price'];
            $total_count++;
        }
        $this->json_result(
                array(
            'total_price' => $total_price,
            'total_count' => $total_count,
                ), 'addto_cart_successed');
    }

    /**
     * 组合推荐显示
     */
    function max_goods_add() {
        $spec_ids = isset($_GET['goods_mix_ids']) ? $_GET['goods_mix_ids'] : 0;
        if (empty($spec_ids)) {
            return;
        }
        $spec_ids = explode(",", $spec_ids);
        $spec_model = & m('goodsspec');

        foreach ($spec_ids as $key => $spec_id) {

            $quantity = 1;
            if (!$spec_id) {
                continue;
            }

            /* 是否有商品 */
            $spec_model = & m('goodsspec');
            $spec_info = $spec_model->get(array(
                'fields' => 'g.spec_qty,g.store_id, g.if_open, g.goods_id, g.goods_name, g.spec_name_1, g.spec_name_2, g.spec_name_3, g.spec_name_4, g.default_image, gs.spec_1, gs.spec_2, gs.spec_3, gs.spec_4, gs.stock, gs.price',
                'conditions' => $spec_id,
                'join' => 'belongs_to_goods',
            ));

            if (!$spec_info) {
                continue;
            }

            /* 如果是自己店铺的商品，则不能购买 */
            if ($this->visitor->get('manage_store')) {
                if ($spec_info['store_id'] == $this->visitor->get('manage_store')) {
                    $this->json_error('can_not_buy_yourself');
                    return;
                }
            }

            /* 是否添加过 */
            $cart_mod = & m('cart');
            $item_info = $cart_mod->get("spec_id={$spec_id} AND session_id='" . SESS_ID . "'");
            if (!empty($item_info)) {
                continue;
            }

            if ($quantity > $spec_info['stock']) {
                continue;
            }


        $spec_1 = $spec_info['spec_name_1'] ? $spec_info['spec_name_1'] . ':' . '<img src="'.str_replace('goodsspec/','goodsspec/spec_',$spec_info['spec_1']).'">' : $spec_info['spec_1'];
        $spec_2 = $spec_info['spec_name_2'] ? $spec_info['spec_name_2'] . ':' . $spec_info['spec_2'] : $spec_info['spec_2'];
		$spec_3 = $spec_info['spec_name_3'] ? $spec_info['spec_name_3'] . ':' . $spec_info['spec_3'] : $spec_info['spec_3'];
		$spec_4 = $spec_info['spec_name_4'] ? $spec_info['spec_name_4'] . ':' . $spec_info['spec_4'] : $spec_info['spec_4'];

        ($spec_info['spec_qty']>0) && $specification = $spec_1 . ' ' . $spec_2 . ' ' . $spec_3 . ' ' . $spec_4;

            //当用多个促销条件存在时候， 此方法用来处理具体使用哪一个价格
            $spec_info = $cart_mod->get_spec_price($spec_info,$this->visitor->get('user_id'));

            /* 将商品加入购物车 */
            $cart_item = array(
                'user_id' => $this->visitor->get('user_id'),
                'session_id' => SESS_ID,
                'store_id' => $spec_info['store_id'],
                'spec_id' => $spec_id,
                'goods_id' => $spec_info['goods_id'],
                'goods_name' => addslashes($spec_info['goods_name']),
                'specification' => addslashes(trim($specification)),
                'price' => $spec_info['price'],
                'quantity' => $quantity,
                'goods_image' => addslashes($spec_info['default_image']),
                'promotions_id' => $spec_info['promotions_id'],
                'goods_old_price' => $spec_info['goods_old_price'],
                'goods_type' => $spec_info['goods_type'],
            );

            /* 添加并返回购物车统计即可 */
            $cart_model = & m('cart');
            $cart_model->add($cart_item);

            /* 更新被添加进购物车的次数 */
            $model_goodsstatistics = & m('goodsstatistics');
            $model_goodsstatistics->edit($spec_info['goods_id'], 'carts=carts+1');
        }

        $cart_status = $this->_get_cart_status();
        $this->json_result(array(
            'cart' => $cart_status['status'], //返回购物车状态
                ), 'addto_cart_successed');
    }

}

?>
